package com.xunwulian.web.controller.buss;

import com.xunwulian.base.dao.SmsGprsBalanceDao;
import com.xunwulian.base.entity.Page;
import com.xunwulian.base.entity.SmsGprsBalanceEntity;
import com.xunwulian.base.entity.User;
import com.xunwulian.base.util.PageData;
import com.xunwulian.base.util.UserUtil;
import com.xunwulian.web.controller.BaseController;
import com.xunwulian.web.util.Const;
import com.xunwulian.web.util.JurisdictionHelper;
import com.xunwulian.web.util.ObjectExcelView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/smsgprsbalance")
public class SmsgprsbalanceController extends BaseController {

    String menuUrl = "smsgprsbalance/list.do"; // 菜单地址(权限用)

    @Autowired
    private SmsGprsBalanceDao smsGprsBalanceDao;

    /**
     * 列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(Page page, String searchKey) {
        logBefore(logger, "列表Smsgprsbalance");

        ModelAndView mv = this.getModelAndView();

        // 从session获取用户信息
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            String userId = user.getUSER_ID();
            // 1和2是系统管理员
            if (UserUtil.isAdmin(user.getROLE_ID())
                    || UserUtil.isSubAdmin(user.getROLE_ID())) {
                userId = null;
            }

            List<SmsGprsBalanceEntity> varList = smsGprsBalanceDao.listPage(page, searchKey, userId); // 列出Smsgprsbalance列表
            mv.setViewName("system/smsgprsbalance/smsgprsbalance_list");
            mv.addObject("varList", varList);
            mv.addObject("searchKey", searchKey);

            mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限

        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }


    /**
     * 导出EXCEL
     *
     * @return
     */
    @RequestMapping(value = "/excel")
    public ModelAndView exportExcel() {
        logBefore(logger, "导出Smsgprsbalance到excel");

        if (!JurisdictionHelper.buttonJurisdiction(menuUrl, "cha")) {
            return null;
        }

        ModelAndView mv = new ModelAndView();

        // 从session获取用户信息
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        String userId = user.getUSER_ID();
        // 1和2是系统管理员
        if ((UserUtil.isAdmin(user.getROLE_ID()) || UserUtil.isSubAdmin(user.getROLE_ID()))) {
            userId = null;
        }

        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("日期"); // 1
        titles.add("短信"); // 2
        titles.add("流量"); // 3
        titles.add("余额"); // 4
        titles.add("卡号"); // 5
        dataMap.put("titles", titles);

        List<PageData> varList = new ArrayList<PageData>();

        Page page = new Page();
        page.setCurrentPage(1);
        page.setShowCount(65535);
        List<SmsGprsBalanceEntity> list = smsGprsBalanceDao.listPage(page, null, userId);

        for (SmsGprsBalanceEntity entity : list) {
            PageData vpd = new PageData();
            vpd.put("var1", entity.getDate()); // 1
            vpd.put("var2", entity.getSms()); // 2
            vpd.put("var3", entity.getGprs()); // 3
//                vpd.put("var4", entity.getBalance()); // 4
            vpd.put("var5", entity.getMsisdn()); // 5
            varList.add(vpd);
        }

        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();

        mv = new ModelAndView(erv, dataMap);
        return mv;
    }

    /**
     * @return
     */
    public Map<String, String> getHC() {
        Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
        Session session = currentUser.getSession();
        return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
    }


    /**
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format,
                true));
    }
}
